package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.MessageConstant;
import com.itheima.common.R;
import com.itheima.domain.Checkitem;
import com.itheima.domain.Result;
import com.itheima.service.CheckihemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/checkitem")
public class CheckitemController {
    @Autowired
    private CheckihemService checkihemService;
    @GetMapping("/page")
    public R getByPage(int page,int pageSize,String name){
        Page page1=new Page(page,pageSize);
        LambdaQueryWrapper<Checkitem> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(Checkitem::getCode);
        return R.success(page1);
    }
    /**
     * 删除本行数据
     * @param id
     * @return
     */
    @DeleteMapping("/delete")
    public R<String> deleteById(Integer id){
        //作删除操作
        checkihemService.removeWithAnother(id);

        return R.success("删除成功！");
    }

    /**
     * 根据id进行查询进行修改的数据回显
     * @param id
     * @return
     */
    @GetMapping("/findById")
    public R<Checkitem> fingById(Integer id){
        //根据id进行查询
        Checkitem checkitem = checkihemService.getById(id);
        if(checkitem==null){
            return R.error("查无此数据，请重新进行查询");
        }
        return R.success(checkitem);
    }
    @RequestMapping("/edit")
    public Result edit(@RequestBody Checkitem checkitem){
        try {
            checkihemService.edit(checkitem);
        }catch (Exception e){
            return new Result(false, MessageConstant.EDIT_CHECKGROUP_FAIL);
        }
        return new Result(true,MessageConstant.EDIT_CHECKGROUP_SUCCESS);
    }

    @RequestMapping("/findById")
    @ResponseBody
    public Result findById(Integer id) {
        try {
            Checkitem checkItem = CheckihemService.findById(id);
            return new Result(true,
                    MessageConstant.QUERY_CHECKITEM_SUCCESS, checkItem);
        } catch (Exception e) {
            e.printStackTrace();
            //服务调用失败
            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }
    @PostMapping("/add")
    public R<String> save(@RequestBody Checkitem checkitem){
        checkihemService.save(checkitem);
        return R.success("新增成功");
    }

    /**
     *  @author: QAQ
     *  @date:   2021/12/13
     *  @description: 根据id删除
     */
    @DeleteMapping("/{id}")
    public R<String> delete(@PathVariable int id){
        checkihemService.removeById(id);
        return R.success("删除成功");
    }

}
